<script setup>
import { ref, defineProps } from 'vue';
import {useRouter} from "vue-router";

const props = defineProps({
  game: Object
})

// 默认的 post 图片 URL
const defaultPostUrl = ref(props.game.post);
let timeoutId = null;

// 当鼠标悬停时更新 defaultPostUrl 的函数
const updateDefaultPost = (url) => {
  if (timeoutId) {
    clearTimeout(timeoutId);
    timeoutId = null;
  }
  defaultPostUrl.value = url;
};

// 当鼠标移开时恢复 defaultPostUrl 为默认值的函数，带延迟
const resetDefaultPost = () => {
  timeoutId = setTimeout(() => {
    defaultPostUrl.value = props.game.post;
  }, 500); // 延迟0.5秒
};

// 游戏详情页
const router = useRouter()
const gameDetail = () => {
  router.push(`/store/detail/${props.game.id}`)
}
</script>

<template>
  <div class="game-container" @click="gameDetail">
    <div class="left">
      <img class="default-post" :src="defaultPostUrl">
    </div>
    <div class="right">
      <div class="game-name ellipsis">{{ game.chineseName }}</div>
      <div class="game-preview">
        <img
            v-for="(item) in game.imageView"
            :key="item"
            class="view"
            @mouseover="updateDefaultPost(item)"
            @mouseleave="resetDefaultPost"
            v-img-lazy="item">
      </div>
      <div class="game-score">
        <span>游戏评分：</span>
        <span>{{ game.rate }}</span>
      </div>
      <div class="game-price">
        <template v-if="game.gamePrice.currentPrice === '0.00'">
          免费开玩
        </template>
        <template v-else>
          <template v-if="game.gamePrice.discount === 0">
            ￥{{ game.gamePrice.originPrice }}
          </template>
          <template v-else>
            <div class="discount">
              <div class="discount-percent">-{{game.gamePrice.discount}}%</div>
              <div class="discount-price">
                <span class="discount-origin-price">￥{{ game.gamePrice.originPrice }}</span>
                <span class="discount-current-price">￥{{ game.gamePrice.currentPrice }}</span>
              </div>
            </div>
          </template>
        </template>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.game-container {
  display: flex;
  background-color: $textColor;
  cursor: pointer;
  box-shadow: 0 0 8px rgba(0, 0, 0, 1);

  .left {
    .default-post {
      width: 616px;
      height: 353px;
      transition: transform 0.3s ease, box-shadow 0.3s ease;

      &:hover {
        transform: scale(102%);
        box-shadow: 0 0 8px rgba(0, 0, 0, 1);
      }
    }
  }

  .right {
    width: 360px;
    padding: 6px 6px 6px 20px;
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 1);
    color: #cccccc;

    .game-name {
      font-size: 26px;
      line-height: 60px;
    }

    .game-preview {
      width: 370px;
      height: 150px;
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
      align-content: space-between;

      img {
        width: 170px;
        height: 69px;
        object-fit: cover;
        object-position: center;
        filter: brightness(50%);
        transition: filter 0.3s ease;
        box-shadow: 0 0 8px rgba(0, 0, 0, 1);

        &:hover {
          filter: brightness(100%);
          cursor: pointer;
        }
      }
    }
    .game-score {
      font-size: 16px;
      span:last-child {
        color: $themeColor;
      }
    }
    .game-price {
      margin-bottom: 10px;
      .discount {
        display: flex;
        font-size: 14px;
        .discount-percent {
          padding: 0 4px;
          background-color: $discountBackgroundColor;
          color: $discountTextColor;
          text-shadow: 1px 1px 4px rgba(0, 0, 0, 1);
        }
        .discount-price {
          text-align: center;
          background-color: #525252;
          .discount-origin-price {
            margin: 0 4px;
            position: relative;
            color: #738895;
            &:before {
              content: '';
              left: 0;
              right: 0;
              position: absolute;
              top: 43%;
              border-bottom: 1.5px solid #738895;
              transform: skewY(-8deg);
              box-shadow: 0 0 2px black;
            }
          }
          .discount-current-price {
            padding: 0 4px 0 0;
          }
        }
      }
    }
  }
}
</style>
